#include "frame.h"

#include <cstdlib>
#include <cstdio>
#include <iostream>

using namespace std;

#include "tinyxml.h"
#include "xml_parser.h"
#include "misc.h"
#include "ExtendedPlotting.h"

frame::frame()
{
 show_me = false;
 plot_vessels = false;
 outline_tumor = false;
 
 DataName = NULL;
 DataSource = NULL;
 Label = NULL;
 min_scale = 0;
 max_scale = 0;
 Use_MinMax_scale = true;
 
 special_blood_vessel_plot = false;
 
 colorbar = false;
 colorbar_labels = false;
 position_x = 0;
 position_y = 0;
  
 color_function = RainbowFunction;
 draw_colorbar_function = DrawRainbowColorbar;
 reverse_colors = false;
 
 vessel_color.Red   = 128;
 vessel_color.Green = 0;
 vessel_color.Blue  = 0;
 
 tumor_outline_color.Red   = 0;
 tumor_outline_color.Green = 0;
 tumor_outline_color.Blue  = 0;
}

frame::~frame()
{
 delete DataName;
 delete DataSource;
}

void frame::display( void )
{
 cout << "show_me : " << (int) show_me << endl
      << "plot vessels: " << (int) plot_vessels << endl
	  << "plot tumor outline: " << (int) outline_tumor << endl
      << "DataName : ";
	  if( DataName ){ cout << DataName; }
 cout << endl << "DataSource : ";
      if( DataSource ){ cout << DataSource; }
 cout << endl << "scale: " << min_scale << " to " << max_scale << endl
	  << "colorbar: " << (int) colorbar << endl
	  << "colorbar_labels: " << (int) colorbar_labels << endl
	  << "vessel color: (" << (int) vessel_color.Red << "," 
	                       << (int) vessel_color.Green << "," 
						   << (int) vessel_color.Blue << ")" << endl
	  << "tumor color: (" << (int) tumor_outline_color.Red << "," 
						  << (int) tumor_outline_color.Green << "," 
						  << (int) tumor_outline_color.Blue << ")" << endl << endl;
	  
 return;
}

bool frame::SetDataName( const char* input )
{
 if( DataName )
 { delete [] DataName; }
 DataName = new char [strlen(input)];
 strcpy( DataName, input );
 return true;
}

bool frame::SetDataSource( const char* input )
{
 if( DataSource )
 { delete [] DataSource; }
 DataName = new char [strlen(input)];
 strcpy( DataSource, input );
 return true;
}

bool frame::SetFromTinyXml( TiXmlElement* Root )
{
 if( !Root )
 { return false; }
 show_me = GetBoolChildElement( Root, "show_me" );
 plot_vessels = GetBoolChildElement( Root, "plot_vessels" );
 outline_tumor = GetBoolChildElement( Root, "outline_tumor" ); 
 
 if( DataName )
 { delete [] DataName; }
 DataName = GetStringChildElement( Root , "data" );

 if( Label )
 { delete [] Label; }
 Label = GetStringChildElement( Root , "label" );
 
 min_scale = GetDoubleChildElement( Root, "min_scale" ); 
 max_scale = GetDoubleChildElement( Root, "max_scale" ); 

 Use_MinMax_scale = GetBoolChildElement( Root, "use_min_max_scale" );

 special_blood_vessel_plot = GetBoolChildElement( Root, "special_blood_vessel_plot" );
 
 colorbar = GetBoolChildElement( Root, "colorbar" );
 colorbar_labels = GetBoolChildElement( Root, "colorbar_labels" );
 
 char* TempString; 
 TempString = GetStringChildElement( Root, "color_function" );
 bool FunctionSet = false;
 
 if( strstr( TempString, "ReverseRainbow" ) && !FunctionSet )
 {
  color_function = RainbowFunction;
  draw_colorbar_function = DrawRainbowColorbar;
  
  reverse_colors = true;

  FunctionSet = true; 
 }
 
 if( strstr( TempString, "Rainbow" ) && !FunctionSet )
 {
  color_function = RainbowFunction;
  draw_colorbar_function = DrawRainbowColorbar;
  FunctionSet = true; 
 }

 if( strstr( TempString, "ReverseGrayscale" ) && !FunctionSet )
 {
  color_function = GrayscaleFunction;
  draw_colorbar_function = DrawGrayscaleColorbar;

  reverse_colors = true;

  FunctionSet = true; 
 }
 
 if( strstr( TempString, "Grayscale" ) && !FunctionSet )
 {
  color_function = GrayscaleFunction;
  draw_colorbar_function = DrawGrayscaleColorbar;

  FunctionSet = true; 
 }

 delete [] TempString;
 TempString = GetStringChildElement( Root, "vessel_color" );
 if( strlen( TempString ) > 1 )
 { vessel_color = OrderedTripleToRGBApixel( TempString ); }

 delete [] TempString;
 TempString = GetStringChildElement( Root, "tumor_outline_color" );
 if( strlen( TempString ) > 1 )
 { tumor_outline_color = OrderedTripleToRGBApixel( TempString ); }
 
 return true;
}

bool frame::SetDataSource( double Time )
{
 if( !DataName )
 { return false; }
 
 if( DataSource )
 delete [] DataSource; 
 
 DataSource = CreateFilename( DataName , Time );
 return true;
}
